package cn.hy.pic.controler;


import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.PageUtil;
import cn.hy.common.Result;
import cn.hy.pic.domain.vo.PictureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/pic")
public class IndexController {

    @Autowired
    Environment env;

    @Value("${file.base_path}")
    String fileBasePath;


    TimedCache<String, List<PictureVO>> cache = CacheUtil.newTimedCache(1000 * 60 * 60);


    @GetMapping("/list")
    public Result<List<PictureVO>> listPic() {
        List<PictureVO> list = getPictureVOS();
        return new Result<List<PictureVO>>().ok(list);
    }


    @GetMapping("/page")
    public Result<List<PictureVO>> pagePic(@RequestParam(value = "pageNo") Integer pageNo, @RequestParam(value = "pageSize") Integer pageSize) {
        List<PictureVO> pictureVOS = cache.get("allFiles");
        if (CollUtil.isEmpty(pictureVOS)) {
            pictureVOS = getPictureVOS();
            cache.put("allFiles", pictureVOS);
        }
        int start = PageUtil.getStart(pageNo, pageSize);
        int end = PageUtil.getEnd(pageNo, pageSize);
        List<PictureVO> sub = ListUtil.sub(pictureVOS, start, end);
        return new Result<List<PictureVO>>().ok(sub);
    }


    private List<PictureVO> getPictureVOS() {
        String port = env.getProperty("server.port");
        List<PictureVO> list = new ArrayList<>();
        List<File> files = FileUtil.loopFiles(fileBasePath, new FileFilter() {
            @Override
            public boolean accept(File file) {
                if (!file.isFile()) {
                    return false;
                }
                return (ImgUtil.IMAGE_TYPE_JPEG.equals(FileUtil.extName(file)) ||
                        ImgUtil.IMAGE_TYPE_JPG.equals(FileUtil.extName(file)) ||
                        ImgUtil.IMAGE_TYPE_PNG.equals(FileUtil.extName(file)) ||
                        "webp".equals(FileUtil.extName(file))
                );
            }
        });
        for (File file : files) {
            PictureVO vo = new PictureVO();
            vo.setTitle(file.getName());
            String absolutePath = file.getAbsolutePath();
            String relateBasePath = absolutePath.substring(fileBasePath.length());
            vo.setFullPath("http://localhost:" + port + "/" + relateBasePath);
            list.add(vo);
        }
        return list;
    }

}
